###################
# MPI_TEAM RAAR 06/12/2208
###################
# You must set the path in order to match the path
# of metascribe

CONTAINER_TDIR			= ../executables
#CONTAINER_TDIR			= /Users/admin/MetaScribe/executables
#CONTAINER_TDIR			= /Vrac/MetaScribe/executables

EXECUTABLE_DIR			= $(CONTAINER_TDIR)/`uname -s`
SOURCES_DIR				= $(CONTAINER_TDIR)/`uname -s`_CMP

METASCRIBE				= $(EXECUTABLE_DIR)/meta_scribe
SHOW_MODEL				= $(EXECUTABLE_DIR)/show_model
XML_EXPORT				= $(EXECUTABLE_DIR)/xml_export
XML_IMPORT				= $(EXECUTABLE_DIR)/xml_import

SEARCH_DIR_OPTION       = -aL$(SOURCES_DIR) -aI$(SOURCES_DIR)

# Directory for our project

GEN=generator
JDT=JDT

#a rediriger vers un fichier .dot
CFLAGS = -Wall -DLOG_TO_DOT
CTIME=-DEXEC_DURATION=$(TIME)

SRC=src
BIN=bin
OBJ=obj
INC=include
RUN=runtime

ifndef $(TIME)
	TIME=20
endif

ifndef $(NAME)
        NAME=NOT_DEFINED
endif

# ADVICE: DEBUG can be use when you call "make".
# It is very useful for more information...

###################################################
# For the question...
###################################################

all : generate

# clean and recompile all
generate: superclean
# Check if the user put the right argument and create the repertories 
# for files. If not, send an error message.
	sh check.sh && make rules-generator && \
	make tengine-generator

# Call metascribe to produce the code source that come from the code generator

rules-generator: 
	$(METASCRIBE) $(DEBUG) anlzed-pn-main.msf generator-main.mssm to-language-main.msst -v "1.0" -a "C_MPI Generator Team"
	gnatchop -w *.ad[sb]
	rm -f smr* syr* smt*
	mv generator_to_language.adb generator_to_language.ads
	mv *.adb *.ads	$(GEN)

# Compilation of the code generator (using sources of metascribe)
tengine-generator: $(GEN)/generator_to_language.ads
	gnatmake $(GEN)/generator_to_language.ads -gnatf \
	$(SEARCH_DIR_OPTION) -bargs -static
	mv *.ali *.o $(GEN)

# launch test
test-generator:  
	@echo -e "\n\nLaunching LAM..."
	lamboot -v bhost
	@echo -e "\n\nCompiling the code..."
	mpicc -Wall $(CTIME) -c $(RUN)/runtime.c -o $(OBJ)/runtime.o \
		-I$(INC) -I$(RUN)
	mpicc -Wall $(CTIME) -c $(SRC)/$(NAME).c -o $(OBJ)/$(NAME).o \
		-I$(INC) -I$(RUN)
	mpicc -Wall $(CTIME) -o $(BIN)/$(NAME) $(OBJ)/$(NAME).o \
		$(OBJ)/runtime.o
	@echo -e "\n\nExecuting the program..."
	mpirun -v -c $(NUM) $(BIN)/$(NAME)
	@echo -e "\n\nClosing LAM..."
	lamwipe -v bhost  
	@echo -e "\n\nDone!"

# for the dot
dot-generator:
	@echo -e "\n\nLaunching LAM..."
	lamboot -v bhost
	@echo -e "\n\nCompiling the code..."
	mpicc $(CFLAGS) $(CTIME) -c $(RUN)/runtime.c -o $(OBJ)/runtime.o \
		-I$(INC) -I$(RUN)
	mpicc $(CFLAGS) $(CTIME) -c $(SRC)/$(NAME).c -o $(OBJ)/$(NAME).o \
	 	-I$(INC) -I$(RUN)
	mpicc $(CFLAGS) $(CTIME) -o $(BIN)/$(NAME) $(OBJ)/$(NAME).o \
		$(OBJ)/runtime.o
	@echo -e "\n\nExecuting the program..."
	mpirun -c $(NUM) $(BIN)/$(NAME) > dot/$(NAME).dot
	@echo -e "\n\nClosing LAM..."
	lamwipe -v bhost  
	@echo -e "\n\nDone!"
	@echo -e "\n\n Visualisation of the dot"
	dotty dot/$(NAME).dot
	@echo -e "\n\nEND!"

# the report
rapport.pdf: *.tex *.eps
	latex rapport.tex
	latex rapport.tex
	dvips -f rapport.dvi > rapport.ps
	ps2pdf rapport.ps
	rm rapport.ps

# test for fk exemple : make test-pour-fk NAME=toto
test-pour-fk: bigclean
	sh check.sh $(NAME) TEST $(TIME)

dot: bigclean
	sh check.sh $(NAME) DOT $(TIME)

###################################################
# AUTRES...
###################################################
# Clean all what we have built
clean:
	rm -f $(GEN)/*.ali $(GEN)/*.o $(OBJ)/*.o *.0 *.log *.aux *.dvi \
	*.toc *.out

# delete the files generated by the compilation
bigclean:
	make clean
	rm -f $(GEN)/*.ads $(GEN)/*.adb $(SRC)/*.c $(INC)/*.h 
	rm -f test_generator.sh $(BIN)/*

# delete the code generator produced by metascribe and the report
superclean:
	make bigclean
	rm -f generator_to_language $(BIN)/* *.xml *.dtd \
	dot/*.dot bhost

# export the MSM into a XML format with his DTD for the example
rdpxml1:
	$(XML_EXPORT) -generic -f anlzed-pn-main.msf -m $(JDT)/anlzed-pn-1-main.msm -o exemple
	ls *.xml *.dtd

rdpxml2:
	$(XML_EXPORT) -generic -f anlzed-pn-main.msf -m $(JDT)/anlzed-pn-2-main.msm -o exemple
	ls *.xml *.dtd

# Have a view of the MSM file (and test it with his MSF)
showinput1: 
	$(SHOW_MODEL) anlzed-pn-main.msf $(JDT)/anlzed-pn-1-main.msm
	cat ShowModel.0
	rm ShowModel.0

showinput2:
	$(SHOW_MODEL) anlzed-pn-main.msf $(JDT)/anlzed-pn-2-main.msm
	cat ShowModel.0
	rm ShowModel.0

# build the distrib for the studient in master
distrib:
	mkdir ARCHIVE
	mkdir ARCHIVE/RAAR-env-projet
	cp frmwrk_msg.ENGLISH *.msm *.msf *.mssm *.msst Makefile ARCHIVE/RAAR-env-projet
	cp rapport.tex logo-sar.eps ARCHIVE/RAAR-env-projet
	cp ../READ_ME-RAAR ARCHIVE
	sh -c '(cd ARCHIVE ;tar czfv ~/Desktop/RAAR-proj-MS.tgz RAAR-env-projet READ_ME-RAAR)'
	rm -rf ARCHIVE
